
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       mav_routing.h
  * @author     baiyang
  * @date       2022-11-4
  ******************************************************************************
  */

#pragma once

#ifdef __cplusplus
extern "C"{
#endif

/*----------------------------------include-----------------------------------*/
#include "gcs_mavlink.h"

#include <stdint.h>
/*-----------------------------------macro------------------------------------*/
// 20 routes should be enough for now. This may need to increase as
// we make more extensive use of MAVLink forwarding
#define MAVLINK_MAX_ROUTES 20
/*----------------------------------typedef-----------------------------------*/
typedef struct mav_routing* mav_routing_t;

struct mav_routing {
    // a simple linear routing table. We don't expect to have a lot of
    // routes, so a scalable structure isn't worthwhile yet.
    uint8_t num_routes;
    struct route {
        uint8_t sysid;
        uint8_t compid;
        mavlink_channel_t channel;
        uint8_t mavtype;
    } routes[MAVLINK_MAX_ROUTES];

    // a channel mask to block routing as required
    uint8_t no_route_mask;
};
/*----------------------------------variable----------------------------------*/

/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
mav_routing_t mavrouting_get_singleton();

/*
  forward a MAVLink message to the right port. This also
  automatically learns the route for the sender if it is not
  already known.

  This returns true if the message should be processed locally
*/
bool mavrouting_check_and_forward(mavlink_channel_t in_channel, const mavlink_message_t *msg);

/*
  send a MAVLink message to all components with this vehicle's system id

  This is a no-op if no routes to components have been learned
*/
void mavrouting_send_to_components(uint32_t msgid, const char *pkt, uint8_t pkt_len);

/*
  search for the first vehicle or component in the routing table with given mav_type and retrieve it's sysid, compid and channel
  returns true if a match is found
 */
bool mavrouting_find_by_mavtype(uint8_t mavtype, uint8_t *sysid, uint8_t *compid, mavlink_channel_t *channel);

/*
  search for the first vehicle or component in the routing table with given mav_type and component id and retrieve its sysid and channel
  returns true if a match is found
 */
bool mavrouting_find_by_mavtype_and_compid(uint8_t mavtype, uint8_t compid, uint8_t *sysid, mavlink_channel_t *channel);
/*------------------------------------test------------------------------------*/

#ifdef __cplusplus
}
#endif



